package gui;

import javax.swing.JPanel;

import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AssignInspectorPanel extends JPanel implements ChatIF
{
	private JTextField deadlineTextField;
	private JTextField inspectorIDtextField;
	private JComboBox inspectionBox;
	private JComboBox executionBox;
	private AcademicFrame frame;
	private CommitteeMemberPanel committeeMemberPanel;
	private ArrayList<Object> deadlineArrayList;
	int indexRequest;
	int indexInspector;
	private ArrayList<Object> inspectorsArrayList;
	private AssignInspectorPanel myself;
	
	public AssignInspectorPanel(AcademicFrame frame, CommitteeMemberPanel committeeMemberPanel)
	{
		this.frame = frame;
		this.myself = this;
		this.committeeMemberPanel = committeeMemberPanel;
		
		initialize();
		
		ArrayList<Object> getListsForInpection = new ArrayList<Object>();
		getListsForInpection.add("getListsForInpection");
		AcademicFrame.myclient.sendMessageToServer(getListsForInpection);
		
	}
	
		private void initialize()
		
		{
		setLayout(null);
		
		JLabel lblAssignInspector = new JLabel("Assign Inspector");
		lblAssignInspector.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblAssignInspector.setBounds(271, 35, 158, 20);
		add(lblAssignInspector);
		
		JLabel lblExecutionToInspect = new JLabel("Execution to inspect");
		lblExecutionToInspect.setBounds(47, 92, 114, 14);
		add(lblExecutionToInspect);
		
		executionBox = new JComboBox();
		executionBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				indexRequest = executionBox.getSelectedIndex();
				if (indexRequest == 0)
				{
					// Do nothing
				}
				else
				{
					deadlineTextField.setText((String)deadlineArrayList.get(indexRequest));
				
				}
				
			}
		});
		executionBox.setBounds(171, 88, 130, 22);
		add(executionBox);
		
		JLabel lblChooseInspector = new JLabel("Choose Inspector");
		lblChooseInspector.setBounds(383, 92, 97, 14);
		add(lblChooseInspector);
		
		inspectionBox = new JComboBox();
		inspectionBox.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				indexInspector = inspectionBox.getSelectedIndex();
				if (indexInspector == 0)
				{
					// Do nothing
				}
				else
				{
					inspectorIDtextField.setText((String)inspectorsArrayList.get(indexInspector-1));
					
				}
				
			}
		});
		inspectionBox.setBounds(490, 88, 130, 22);
		add(inspectionBox);
		
		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				frame.setSize(500, 500);
				frame.setContentPane(committeeMemberPanel);		
			}
		});
		btnClose.setBounds(210, 277, 91, 23);
		add(btnClose);
		
		JButton btnAssign = new JButton("Assign");
		btnAssign.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				if ( inspectionBox.getSelectedItem().toString().equalsIgnoreCase("--Choose an Inspector--") ) 
				{
					(new Dialog("Please choose an Inspector first.")).setVisible(true);
				}
				else
					if ( executionBox.getSelectedItem().toString().equalsIgnoreCase("--Choose a request--") )
					{
						(new Dialog("Please choose a request first.")).setVisible(true);
					}
					else
					{
						String inspectorID = (String)inspectorsArrayList.get(indexInspector-1);
						String requestID = executionBox.getSelectedItem().toString();
						
						ArrayList<Object> assignInspector = new ArrayList<Object>();
						assignInspector.add("assignInspector");
						assignInspector.add(inspectorID);
						assignInspector.add(requestID);
						
						
						// Remove items from the list and the values that correspond to them in the deadline and uid arraylist
			
						deadlineArrayList.remove(executionBox.getSelectedIndex());
						executionBox.removeItemAt(executionBox.getSelectedIndex());
						myself.repaint();
						
						//Send to the server to create the pair
						
						AcademicFrame.myclient.sendMessageToServer(assignInspector);
						
					}
					
				
			}
		});
		btnAssign.setBounds(383, 277, 91, 23);
		add(btnAssign);
		
		JLabel lblDeadline = new JLabel("Deadline");
		lblDeadline.setBounds(106, 146, 46, 14);
		add(lblDeadline);
		
		deadlineTextField = new JTextField();
		deadlineTextField.setEditable(false);
		deadlineTextField.setBounds(177, 143, 86, 20);
		add(deadlineTextField);
		deadlineTextField.setColumns(10);
		
		JLabel lblInspectorId = new JLabel("Inspector ID");
		lblInspectorId.setBounds(394, 146, 76, 14);
		add(lblInspectorId);
		
		inspectorIDtextField = new JTextField();
		inspectorIDtextField.setEditable(false);
		inspectorIDtextField.setColumns(10);
		inspectorIDtextField.setBounds(490, 143, 86, 20);
		add(inspectorIDtextField);
		
	
	}

	@Override
	public void display(Object message) 
	{
	
		
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("listOfRequestsToAssign"))
		{
			int numberOfItemsInAssignment = Integer.parseInt((String)answer.get(1)); // get the amount of items to appear in the to be assigned combobox
			this.executionBox.addItem("--Choose a request--");
			
			for (int i=0; i < numberOfItemsInAssignment ; i++ )
			{
				
				this.executionBox.addItem((String)((ArrayList<Object>)(answer.get(2))).get(2+i));  // add requests to the list
			}
			this.deadlineArrayList = (ArrayList<Object>)(answer.get(3)); // ArrayList of the deadlines
			
			//Get list of inspectors
			
			ArrayList<Object> getUsersToAssign = new ArrayList<Object>();
			getUsersToAssign.add("getUsersToAssign");
			getUsersToAssign.add("CommitteeMember");
			AcademicFrame.myclient.sendMessageToServer(getUsersToAssign);
					
		}
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("listOfUsersWithSkill"))
			{
				if (	((String)(answer.get(1))).equalsIgnoreCase("Valid"))
				{
					int numberOfItemsInUsers = Integer.parseInt((String)answer.get(2)); // get the amount of items to appear in the to be inspectors combobox
					this.inspectionBox.addItem("--Choose an Inspector--");
					
					for (int i=0; i < numberOfItemsInUsers ; i++ )
					{
						
						this.inspectionBox.addItem((String)((ArrayList<Object>)(answer.get(4))).get(i));  // add inspectors to the list
					}
					this.inspectorsArrayList = (ArrayList<Object>)(answer.get(3)); // ArrayList of the inspectors (Uid)
					
				}
				else
				{
					// empty list
				}
				
			}
			else
				if (	((String)(answer.get(0))).equalsIgnoreCase("inspectorAssigned"))
				{
					(new Dialog("Inspector assigned successfuly.")).setVisible(true);
				}
		
			
		
	}
}
